package com.rchat.pocmini.activity.groupmember;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.airtalkee.sdk.AirtalkeeAccount;
import com.airtalkee.sdk.AirtalkeeContactPresence;
import com.airtalkee.sdk.entity.AirContact;
import com.rchat.pocmini.R;
import com.rchat.pocmini.bean.QueryGroupMemberBean;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/9/8.
 */

public class GroupMemberAdapter extends BaseAdapter{

    private ArrayList<QueryGroupMemberBean.DataBean.ChatMembersBean> data;
    private LayoutInflater inflater;

    public GroupMemberAdapter(Context context,ArrayList<QueryGroupMemberBean.DataBean.ChatMembersBean> data){
        this.data = data;
        inflater  = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.search_listview_item, null);
            holder = new ViewHolder();
            holder.ivHead = (ImageView) convertView.findViewById(R.id.imageView1);
            holder.tvDisplayName = (TextView) convertView.findViewById(R.id.textView1);
            holder.tvUserName = (TextView) convertView.findViewById(R.id.textView2);
            holder.cb = (CheckBox) convertView.findViewById(R.id.cb_selectmenbes);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        QueryGroupMemberBean.DataBean.ChatMembersBean bean = data.get(position);
        switch (AirtalkeeContactPresence.getInstance().getContactStateById(bean.getUserName())) {
            case AirContact.CONTACT_STATE_ONLINE:
                holder.ivHead.setImageResource( R.drawable.member_head_online);
                break;
            default:
                holder.ivHead.setImageResource( R.drawable.member_head_offline);
        }
        if(bean.getUserName().equals(AirtalkeeAccount.getInstance().getUserId())){
            holder.ivHead.setImageResource( R.drawable.member_head_online);
        }
        holder.cb.setVisibility(View.GONE);
        holder.tvDisplayName.setText(bean.getDisplayName());
        holder.tvUserName.setText(bean.getUserName());
        return convertView;
    }

    public static class ViewHolder {
        private ImageView ivHead;
        private TextView tvDisplayName;
        private TextView tvUserName;
        private CheckBox cb;
    }
}
